﻿using System.Diagnostics;

namespace SyncAndAsync
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            var syncStopwatch = Stopwatch.StartNew();
            SyncMethod();
            syncStopwatch.Stop();
            Console.WriteLine($"同步方法耗时: {syncStopwatch.Elapsed}");


            var asyncStopwatch = Stopwatch.StartNew();
            await AsyncMethod1();
            asyncStopwatch.Stop();
            Console.WriteLine($"异步方法异步执行耗时: {asyncStopwatch.Elapsed}");

            var asyncStopwatch1 = Stopwatch.StartNew();
            await AsyncMethod2();
            asyncStopwatch1.Stop();
            Console.WriteLine($"异步方法等待结果执行耗时: {asyncStopwatch1.Elapsed}");

            Console.ReadLine();
        }

        /// <summary>
        /// 同步方法
        /// </summary>
        static void SyncMethod()
        {
            Thread.Sleep(1000);

            Thread.Sleep(1000);

            Thread.Sleep(1000);
        }

        /// <summary>
        /// 异步方法
        /// </summary>
        static async Task AsyncMethod1()
        {
            var task1 = Task.Delay(1000);

            var task2 = Task.Delay(1000);

            var task3 = Task.Delay(1000);

            await Task.WhenAll(task1, task2, task3);
        }

        /// <summary>
        /// 异步等待结果方法
        /// </summary>
        static async Task AsyncMethod2()
        {
            await Task.Delay(1000);

            await Task.Delay(1000);

            await Task.Delay(1000);
        }
    }
}